package com.yycx.module.bus.provider.handler.haiying;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.hy.BusHyorderReturn;
import com.yycx.module.bus.client.entity.hy.BusPaymentOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单明细拆分工具类
 */
public class HyOrderDetailsSplit {

    /**
     * 根据回款拆分订单明细
     *
     * @param orderDetails  订单明细金额必须倒序
     * @param paymentOrders 收款金额必须正序排序
     * @return
     */
    public static List<BusHyorderReturn> split(List<HyOrderDetail> orderDetails, List<BusPaymentOrder> paymentOrders) {
        List<BusHyorderReturn> result = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(orderDetails)) {
            for (HyOrderDetail orderDetail : orderDetails) {
                BigDecimal saleAmount = orderDetail.getSaleAmount();
                orderDetail.setSurplusAmount(saleAmount);
                BigDecimal amount = orderDetail.getSurplusAmount();
                if (FlymeUtils.isNotEmpty(paymentOrders) && FlymeUtils.gtzero(amount)) {
                    for (BusPaymentOrder paymentOrder : paymentOrders) {
                        BigDecimal takeAmount = paymentOrder.getUseAmount();
                        //优先匹配回款金额和发货金额一致的记录
                        if (amount.equals(takeAmount)) {
                            BusHyorderReturn item = getHyOrderDetail(orderDetail, paymentOrder, amount);
                            orderDetail.setSurplusAmount(new BigDecimal("0"));
                            paymentOrder.setUseAmount(new BigDecimal("0"));
                            result.add(item);
                        }
                    }
                }
            }
        }
        if (FlymeUtils.isNotEmpty(orderDetails)) {
            splitOther(result, orderDetails, paymentOrders);
        }
        return result;
    }

    /**
     * 继续拆分发货金额和回款金额不一致的
     *
     * @param result
     * @param orderDetails
     * @param paymentOrders
     * @return
     */
    private static List<BusHyorderReturn> splitOther(List<BusHyorderReturn> result, List<HyOrderDetail> orderDetails, List<BusPaymentOrder> paymentOrders) {
        if (FlymeUtils.isNotEmpty(orderDetails)) {
            boolean tag = false;
            for (HyOrderDetail orderDetail : orderDetails) {
                BigDecimal amount = orderDetail.getSurplusAmount();
                if (FlymeUtils.isNotEmpty(paymentOrders) && FlymeUtils.gtzero(amount)) {
                    for (BusPaymentOrder paymentOrder : paymentOrders) {
                        BigDecimal takeAmount = paymentOrder.getUseAmount();
                        if (FlymeUtils.gtzero(takeAmount)) {
                            //如果发货金额大于收款金额
                            if (FlymeUtils.ge(amount, takeAmount)) {
                                //计算当前明细剩余金额
                                amount = amount.subtract(takeAmount);
                                BusHyorderReturn item = getHyOrderDetail(orderDetail, paymentOrder, takeAmount);
                                result.add(item);
                                //修改订单明细剩余金额
                                orderDetail.setSurplusAmount(amount);
                                //置空当前回款表示已经用完
                                paymentOrder.setUseAmount(new BigDecimal("0"));
                                //表示需要继续递归
                                tag = true;
                            } else {
                                //如果收款金额大于发货金额
                                takeAmount = takeAmount.subtract(amount);
                                BusHyorderReturn item = getHyOrderDetail(orderDetail, paymentOrder, amount);
                                result.add(item);
                                //当前订单匹配回款已经计算完毕
                                orderDetail.setSurplusAmount(new BigDecimal("0"));
                                //修改当前回款剩余金额
                                paymentOrder.setUseAmount(takeAmount);
                            }
                        }
                    }
                }
            }
            if (tag) {
                splitOther(result, orderDetails, paymentOrders);
            }
        }
        return result;
    }

    /**
     * 获取拆分后的订单明细
     *
     * @param orderDetail
     * @param paymentOrder
     * @param amount
     * @return
     */
    private static BusHyorderReturn getHyOrderDetail(HyOrderDetail orderDetail, BusPaymentOrder paymentOrder, BigDecimal amount) {
        BusHyorderReturn hyorderReturn = new BusHyorderReturn();
        //当前明细总金额
        BigDecimal saleAmount = orderDetail.getSaleAmount();
        BeanUtils.copyProperties(orderDetail, hyorderReturn);
        hyorderReturn.setSaleAmount(amount);
        //金额不一致时需要拆分计算
        if (FlymeUtils.gt(saleAmount, amount)) {
            //数量
            hyorderReturn.setSaleNum(computeAmount(saleAmount, amount, hyorderReturn.getSaleNum()));

            //结算发货金额
            hyorderReturn.setSettleSendAmount(computeAmount(saleAmount, amount, hyorderReturn.getSettleSendAmount()));

            //票折金额
            hyorderReturn.setDiscountPrice(computeAmount(saleAmount, amount, hyorderReturn.getDiscountPrice()));
            //高开金额
            hyorderReturn.setCustomGapUpAmount(computeAmount(saleAmount, amount, hyorderReturn.getCustomGapUpAmount()));
            //资保金收款金额
            hyorderReturn.setCustomBondTakeAmount(computeAmount(saleAmount, amount, hyorderReturn.getCustomBondTakeAmount()));

            //渠道商结算金额
            hyorderReturn.setSettleAmount(computeAmount(saleAmount, amount, hyorderReturn.getSettleAmount()));
            //片区服务商结算金额
            hyorderReturn.setSettleAmountC(computeAmount(saleAmount, amount, hyorderReturn.getSettleAmountC()));

            //商务经理结算金额
            hyorderReturn.setSettleAmountD(computeAmount(saleAmount, amount, hyorderReturn.getSettleAmountD()));
            //商务总负责人结算金额
            hyorderReturn.setSettleAmountE(computeAmount(saleAmount, amount, hyorderReturn.getSettleAmountE()));
            //部门结算金额
            hyorderReturn.setDeptProfitAmount(computeAmount(saleAmount, amount, hyorderReturn.getDeptProfitAmount()));
            //利润
            hyorderReturn.setProfitAmount(computeAmount(saleAmount, amount, hyorderReturn.getProfitAmount()));
            //税收利润
            hyorderReturn.setTaxProfitAmount(computeAmount(saleAmount, amount, hyorderReturn.getTaxProfitAmount()));
            //超期差额
            hyorderReturn.setOverDateAmount(computeAmount(saleAmount, amount, hyorderReturn.getOverDateAmount()));

            //超标准差额
            hyorderReturn.setOverStandAmount(computeAmount(saleAmount, amount, hyorderReturn.getOverStandAmount()));
            //产品利润
            hyorderReturn.setDrugProfitAmount(computeAmount(saleAmount, amount, hyorderReturn.getDrugProfitAmount()));

            //边际利润
            hyorderReturn.setBjProfitAmount(computeAmount(saleAmount, amount, hyorderReturn.getBjProfitAmount()));
            //管理利润
            hyorderReturn.setManageProfitAmount(computeAmount(saleAmount, amount, hyorderReturn.getManageProfitAmount()));
            //预提税金
            hyorderReturn.setYtTaxAmount(computeAmount(saleAmount, amount, hyorderReturn.getYtTaxAmount()));

            //校验金额
            hyorderReturn.setCheckAmount(computeAmount(saleAmount, amount, hyorderReturn.getCheckAmount()));
            //支付开票税金
            hyorderReturn.setPayInvoiceTaxAmount(computeAmount(saleAmount, amount, hyorderReturn.getPayInvoiceTaxAmount()));
            //资保金
            hyorderReturn.setCustomBond(computeAmount(saleAmount, amount, hyorderReturn.getCustomBond()));
            //成本金额
            hyorderReturn.setCostAmount(computeAmount(saleAmount, amount, hyorderReturn.getCostAmount()));
            //拆分比例
            BigDecimal ratio = amount.divide(saleAmount, 3, BigDecimal.ROUND_HALF_UP);
            hyorderReturn.setRadio(ratio);
        }
        hyorderReturn.setReturnAmount(amount);
        hyorderReturn.setReturnId(paymentOrder.getPaymentId());
        hyorderReturn.setReturnBank(paymentOrder.getBankName());
        hyorderReturn.setReturnDate(paymentOrder.getTakeDate());
        hyorderReturn.setReturnNo(paymentOrder.getPaymentNo());
        hyorderReturn.setCustomReturnState(1);
        hyorderReturn.setRenDate(paymentOrder.getRenDate());
        return hyorderReturn;
    }

    /**
     * 按比例计算金额
     *
     * @param saleAmount    总明细金额
     * @param amount        拆分金额
     * @param computeAmount 当前需要计算的字段金额
     * @return
     */
    private static BigDecimal computeAmount(BigDecimal saleAmount, BigDecimal amount, BigDecimal computeAmount) {
        if (FlymeUtils.isNotEmpty(computeAmount) && FlymeUtils.gtzero(computeAmount)) {
            return (computeAmount.multiply(amount)).divide(saleAmount, 5, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return new BigDecimal("0");
    }
}
